package com.atguigu.gulimall.member.controller;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.to.MemberLoginTo;
import com.atguigu.common.to.MemberRegistTo;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.exception.PhoneExistedException;
import com.atguigu.gulimall.member.exception.UsernameExistedException;
import com.atguigu.gulimall.member.feign.CouponFeignService;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author gyhe10
 * @email gyhe10@163.com
 * @date 2024-08-17 12:40:41
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser socialUser) throws Exception {
        MemberEntity memberEntity = memberService.oauthLogin(socialUser);
        if (memberEntity != null) {
            return R.ok().put("data", memberEntity);
        } else {
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }

    /**
     * 会员登录
     * @param to
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginTo to) {
        MemberEntity memberEntity = memberService.login(to);
        if (memberEntity != null) {
            return R.ok().put("data", memberEntity);
        } else {
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }

    /**
     * 会员注册
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistTo to) {
        try {
            memberService.regist(to);
        } catch (PhoneExistedException e) {
            return R.error(BizCodeEnum.MEMBER_PHONE_EXISTED_EXCEPTION.getCode(), BizCodeEnum.MEMBER_PHONE_EXISTED_EXCEPTION.getMsg());
        } catch (UsernameExistedException e) {
            return R.error(BizCodeEnum.MEMBER_USERNAME_EXISTED_EXCEPTION.getCode(), BizCodeEnum.MEMBER_USERNAME_EXISTED_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    /**
     * 测试 Open Feign 远程调用
     * 获取会员的所有优惠券
     */
    @RequestMapping("/coupons")
    public R testFeignClient() {
        // 模拟数据库查询会员信息
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");

        // 远程调用 coupon 微服务，查询会员所有优惠券
        R memberedCoupons = couponFeignService.memberCoupons();
        return R.ok().put("member", memberEntity).put("coupons", memberedCoupons.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
